<?php
/**
 *
 *
 */
namespace app\admin\controller;

use think\Controller;
use think\Db;
use Think\Config;
class Model extends Common
{
    public function index()
    {
        $id = input('id');
        $this->assign('id', $id);
        return view();
    }
    public function edit()
    {
        $id = input('id');
        $this->assign('id', $id);
        return view();
    }
    //添加
    public function insert()
    {
    	h('phpmodelinsertadmin');
        $data = $this->request->post();
        $c = Db::name('model')->where('name', input('name'))->find();
        $list = Db::query('SHOW TABLE STATUS');
        $list = array_map('array_change_key_case', $list);
        foreach ($list as $html) {
            if (prefix().input('name') == $html['name']) {
                $this->error('当前表名重复');
            }
        }
        if(!preg_match("/^[a-zA-Z\s]+$/",input('name'))){
        	$this->error('表名 请输入英文字母组成');
        }
        $data['time'] = time();
        if (!Db::name('model')->data($data)->insert()) {
            $this->error('添加失败');
        } else {
            $db_config = array();
            $db_config['prefix'] = Config::get('database.prefix');
            $sqldata = " DROP TABLE IF EXISTS `".prefix()."".input('name')."`;
            CREATE TABLE `".prefix()."".input('name')."` (
            `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
            `tid` int(11) unsigned NULL DEFAULT '0' COMMENT '上级',
            `model` int(11) unsigned NULL DEFAULT '0' COMMENT '模型',
            `uid` int(11) unsigned NULL DEFAULT '0' COMMENT '用户uid',
            `settop` int(11) unsigned NULL DEFAULT '0' COMMENT '置顶',
            `slide` int(11) unsigned NULL DEFAULT '0' COMMENT '幻灯片',
            `open` int(11) unsigned NULL DEFAULT '1' COMMENT '状态',
            `title` varchar(100) NOT NULL DEFAULT '' COMMENT '标题',
            `view` int(11) unsigned NULL DEFAULT '1' COMMENT '点击量',
            `time` int(11) unsigned NULL DEFAULT '1' COMMENT '添加时间',
            `times` int(11) unsigned NULL DEFAULT '1' COMMENT '修改时间',
            `keywords` varchar(999) NOT NULL DEFAULT '' COMMENT '标签',
            `description` varchar(200) NOT NULL DEFAULT '' COMMENT '描述',
            `content` text NOT NULL COMMENT '内容',
            `pic` text NOT NULL COMMENT '封面图',
            `file` varchar(999) NOT NULL DEFAULT '' COMMENT '',
            `baidu` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '百度推送',
            `xiongzhang` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '熊掌推送',
            `fufei` int(11) unsigned NULL DEFAULT '0' COMMENT '付费阅读',
            `jifen` int(11) unsigned NULL DEFAULT '0' COMMENT '付费积分',
            `zan` int(11) unsigned NULL DEFAULT '0' COMMENT '点赞表',
            PRIMARY KEY (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='".input('title')."表' ;";
            $sql_array = preg_split("/;[\r\n]+/", $sqldata);
            foreach ($sql_array as $k => $v) {
                if (!empty($v)) {
                    Db::query($v);
                }
            }
            $this->success('添加成功');
        }
      
    }
    //更新
    public function update()
    {
    	h('phpmodelupdateadmin');
        $id = input('id');
        $data = $this->request->post();
        $data['time'] = time();
        $data['title'] = xss(input('title'));
        $data['describe'] = xss(input('describe'));
        if (Db::name('model')->where('id', $id)->data($data)->update($data)) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }
    //删除
    public function delete()
    {
    	h('phpmodeldeleteadmin');
        $id = input('id');
        $data = $this->request->post();
        if (m($id)=='html') {
            $this->error('默认模型禁止删除 但是可以隐藏清除数据');
        }
        if (m($id)=='dow') {
            $this->error('默认模型禁止删除 但是可以隐藏清除数据');
        }
        $c = Db::name('model')->where('id', $id)->find();
        if (Db::name('model')->where('id', $id)->data($data)->delete($data)) {
            Db::query('DROP TABLE IF EXISTS  ' . prefix() . '' . $c['name'] . ';');
            Db::name('zan')->where('model', $id)->delete();
            Db::name('xiaoxi')->where('model', $id)->delete();
            Db::name('shoucang')->where('model', $id)->delete();
            Db::name('model_ziduan')->where('tid', $id)->delete();
            Db::name('model_whole')->where('model', $id)->delete();
            $wanneng = Db::name('model')->select();
            foreach ($wanneng as $html) {
                Db::name($html['name'])->where('model', $id)->delete();
            }
            Db::name('comment')->where('model', $id)->delete();
            Db::name('cate')->where('model', $id)->delete();
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
    //清除模型数据
    public function deleteqc()
    {
        $id = input('id');
        $data = $this->request->post();
        $c = Db::name('model')->where('id', $id)->find();
        Db::name($c['name'])->where('model', $id)->delete();
        Db::name('zan')->where('model', $id)->delete();
        Db::name('xiaoxi')->where('model', $id)->delete();
        Db::name('shoucang')->where('model', $id)->delete();
        Db::name('model_whole')->where('model', $id)->delete();
        Db::name('comment')->where('model', $id)->delete();
        $this->success('清除成功');
    }
    
    //字段管理
    public function ziduan()
    {
        $id = input('id');
        $this->assign('id', $id);
        return view();
    }
    //添加字段
    public function ziduanInsert()
    {
    	h('phpmodelziduanInsertadmin');
    	$data = $this->request->post();
        $data['morenzhi'] = '';
        $data['name'] = xss($data['name']);
        $data['title'] = xss($data['title']);
        $id = input('tid');
        $this->assign('id', $id);
        $name = fun_sc_p('model', array('id'=>input('tid')), 'name');
        $c = Db::name('modelZiduan')->where('tid', $id)->where('name', input('name'))->find();
        $azd = 'id,tid,model.uid,settop,jing,open,title,view,time,times,keywords,description,content,pic,file,baidu,xiongzhang,ad,fufei,jifen';
        if(!preg_match("/^[a-zA-Z\s]+$/",input('name'))){
        	$this->error('字段名 请输入英文字母组成');
        }
        if (input('name') == $c['name']||socha($azd,input('name'))==1) {
            $this->error('字段存在');
        } else {
            $data['time'] = time();
            $data['tid'] = $id;
            if (!Db::name('modelZiduan')->data($data)->insert()) {
                $this->error('添加失败');
            } else {
                $db_config = array();
                $db_config['prefix'] = Config::get('database.prefix');
                /*添加字段表*/
                if (input('int')==1) {
                	$sqldata = "ALTER TABLE `".prefix()."".$name."` ADD  `".input('name')."` int(32) NOT NULL DEFAULT '0';";
                } else if (input('int')==2){
                	$sqldata = "ALTER TABLE `".prefix()."".$name."` ADD  `".input('name')."` varchar(999) NOT NULL DEFAULT '".input('morenzhi')."';";
                } else if (input('int')==3){
                	$sqldata = "ALTER TABLE `".prefix()."".$name."` ADD `".input('name')."` TEXT NULL DEFAULT NULL COMMENT '".input('morenzhi')."';";
               	}
                
                
                $sql_array = preg_split("/;[\r\n]+/", $sqldata);
                foreach ($sql_array as $k => $v) {
                    if (!empty($v)) {
                        Db::query($v);
                    }
                }
                $this->success('添加成功');
            }
        }
        return view();
    }
    
    //更新字段页面
    public function ziduanbianji()
    {
    	h('phpmodelziduanUpdateadmin');
        $id = input('id');
        $v = Db::name('model_ziduan')->where('id', $id)->find();
        $this->assign('v', $v);
        if (request()->isPost()) {
        $data = $this->request->post();
        $data['time'] = time();
        $data['title'] = xss($data['title']);
        if (Db::name('modelZiduan')->where('id', $id)->data($data)->update($data)) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
        }
        return view();
    }
    //更新字段
    public function ziduanUpdate()
    {
    	h('phpmodelziduanUpdateadmin');
        $id = input('id');
        $data = $this->request->post();
        $data['time'] = time();
        $data['title'] = xss($data['title']);
        if (Db::name('modelZiduan')->where('id', $id)->data($data)->update($data)) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }
    //删除
    public function ziduanDelete()
    {
    	h('phpmodelziduanDeleteadmin');
        $id = input('id');
        $tid = fun_sc_p('modelZiduan', array('id'=>$id), 'tid');
        $model = fun_sc_p('model', array('id'=>$tid), 'name');
        $name = fun_sc_p('modelZiduan', array('id'=>$id), 'name');
        $data = $this->request->post();
        if (Db::name('modelZiduan')->where('id', $id)->data($data)->delete($data)) {
            Db::query('ALTER TABLE `' . prefix() . '' . $model . '` drop column `'.$name.'`;');
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
    //联动
    public function liandong()
    {
    	h('phpmodelliandongadmin');
        $id = input('id');
        $this->assign('id', $id);
        $c = Db::name('model_ziduan')->where('id', $id)->find();
        $this->assign('c', $c);
        if (request()->isPost()) {
            $data = input('post.');
            if (!empty($_POST['liandongzd'])) {
               $data['liandongzd'] = implode(',', $_POST['liandongzd']);
            } else {
               $data['liandongzd'] = '';
            }
            if (!Db::name('model_ziduan')->data($data)->update()) {
                $this->error('更新失败');
            } else {
                $this->success('更新成功');
            }
            
            
        }
        return view();

    }
    //联动添加更新
    public function liandongadd()
    {
    	h('phpmodelliandongaddadmin');
        if (request()->isPost()) {
            $data = input('post.');
            if (fun_st('model_ziduan_l',array('model'=>input('model'),'ziduan'=>input('ziduan'),'dianji'=>input('dianji')))==0) {
            if (!empty($_POST['yingchang'])) {
               $data['yingchang'] = implode(',', $_POST['yingchang']);
            } else {
               $data['yingchang'] = '';
            }
            if (!Db::name('model_ziduan_l')->data($data)->insert()) {
                $this->error('添加失败');
            } else {
                $this->success('添加成功');
            }
            } else {	
            if (!empty($_POST['yingchang'])) {
               $data['yingchang'] = implode(',', $_POST['yingchang']);
            } else {
               $data['yingchang'] = '';
            }
            if (!Db::name('model_ziduan_l')->where(array('model'=>input('model'),'ziduan'=>input('ziduan'),'dianji'=>input('dianji')))->data($data)->update()) {
                $this->error('更新失败');
            } else {
                $this->success('更新成功');
            }
            }
            
        }

    }
}